new Vue({
    el:'#app',
        data(){
            return{
                msg :null,
                adddata:{
                    id:"",
                    name:"",
                    age:"",
                    hobit:""
                },
                show:false,
                show1:true,
                index1:""
            }
        },
    mounted(){
            axios
                .get('/servletstudy_war_exploded/vueStu')
                .then(response => (this.msg = response.data))
                .catch(function (error) {
                    console.log(error);
                });
    },
    methods:{
        del:function(index){
            if(confirm("确定删除吗")){
                axios
                    .get('/servletstudy_war_exploded/vueStud?id='+this.msg[index].id)
                    .then(response => {
                        if(response.data==="success"){
                            alert("删除成功");
                            this.msg.splice(index, 1)
                        }
                        else{
                            alert("后端问题");
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                    });

            }
        },
        axiosstu:function(){
            axios
                .get('/servletstudy_war_exploded/vueStu')
                .then(response => (this.msg = response.data))
                .catch(function (error) {
                    console.log(error);
                });
        },
        update:function(index){
            this.show=true;
            this.show1=false;

            let data1={};
            data1={...this.msg[index]};
            this.adddata=data1;
            this.index1=index;
        },
        updateok:function(){
            let data2 = encodeURIComponent(JSON.stringify(this.adddata));
            axios
                .get('/servletstudy_war_exploded/vueStuup?data2='+data2)
                .then(response => {
                    if(response.data==="success"){
                        alert("更新成功");
                        this.axiosstu();
                    }
                    else{
                        alert("后端问题");
                    }
                })
                .catch(function (error) {
                    console.log(error);
                });
            this.show1=true;
            this.show=false;
            this.adddata={};
        },
        add:function(){
            for (let i = 1; i < Object.keys(this.adddata).length; i++) {
                if (this.adddata[Object.keys(this.adddata)[i]]==""){
                    alert("数据不完整");
                    return;
                }
            }
            // if (this.adddata.name==""||this.adddata.age=="") {
            // alert("数据不完整");
            //     return;
            // }
            let data={};
            data={...this.adddata};
            let adds = encodeURIComponent(JSON.stringify(data));
            axios
                .get('/servletstudy_war_exploded/vueStuad?data1='+adds)
                .then(response => {
                    if(response.data==="success"){
                        alert("添加成功");
                        this.axiosstu();
                    }
                    else{
                        alert("后端问题");
                    }
                })
                .catch(function (error) {
                    console.log(error);
                });
          //  this.msg.push(data);
          //  this.adddata.id="";
            this.adddata.name="";
            this.adddata.age="";
            this.adddata.hobit="";
        }
    }
})